package cn.nubia.admin.common.ftp;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

/**
 * 有问题找翔
 * Created by wangj on 2014/4/30.
 */
public class FtpClientFactory extends BasePooledObjectFactory<FTPClientWrapper> {

    public FtpClientFactory(String ftpServer, String username, String password) {
        this.ftpServer = ftpServer;
        this.username = username;
        this.password = password;
    }

    public FtpClientFactory(String ftpServer, String username, String password ,Boolean localActive ,Boolean binaryTransfer) {
        this.ftpServer = ftpServer;
        this.username = username;
        this.password = password;
        this.localActive = localActive ;
        this.binaryTransfer = binaryTransfer;
    }

    private String ftpServer;
    private String username;
    private String password;
    private Boolean localActive = false;
    private Boolean binaryTransfer = true;


    public FTPClientWrapper create() {
        return new FTPClientWrapper(ftpServer , username ,password , localActive ,binaryTransfer);
    }

    /**
     * Use the default PooledObject implementation.
     */
    @Override
    public PooledObject<FTPClientWrapper> wrap(FTPClientWrapper clientWrapper) {
        return new DefaultPooledObject<FTPClientWrapper>(clientWrapper);
    }

    @Override
    public void destroyObject(PooledObject<FTPClientWrapper> p)
            throws Exception {
        p.getObject().shutdown();
    }

    /**
     * When an object is returned to the pool, clear the buffer.
     */
    @Override
    public void passivateObject(PooledObject<FTPClientWrapper> pooledObject) {
//        pooledObject.getObject().shutdown();
    }

    public void setFtpServer(String ftpServer) {
        this.ftpServer = ftpServer;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
